package com.simon.lamdba;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @ClassName Main  XX类
 * @Author: Administrator    //作者
 * @CreateDate: 2024-01-09 21:57	//创建时间
 * @Version: 1.0    //版本号
 * @Description: 描述
 */
public class Main {

    public static void main(String[] args) {

        // baseLamdbaUse();


        // compare();

        // new Thread(() -> {
        //     System.out.println(Thread.currentThread().getName());
        // }).start();

        Supplier<String> supplier = () -> "42";

        Predicate<String> isNumber = str -> str.matches("-?\\d+(\\.\\d+)?");
        boolean test = isNumber.test("777");
        System.out.println(test);

        Function<String, Integer> converter = Integer::parseInt;

        Integer apply = converter.apply("2");
        System.out.println(apply);

        Predicate<Integer> isOdd = i -> i % 2 == 1;

        Consumer<Integer> consumer = i -> {
            if (isOdd.test(i)) {
                System.out.println("奇数");
            } else {
                System.out.println("偶数");
            }
        };
        // consumer.accept(3);
        System.out.println("-------------------------------");

        customMethod(supplier, isNumber, converter, consumer);

        System.out.println("-------------------------------");

        customMethod(() -> "889",
                str -> str.matches("-?\\d+(\\.\\d+)?"),
                Integer::parseInt,
                i -> {
                    if (isOdd.test(i)) {
                        System.out.println("奇数");
                    } else {
                        System.out.println("偶数");
                    }
                });
    }

    private static void customMethod(Supplier<String> supplier,
                                     Predicate<String> isNumber,
                                     Function<String, Integer> converter,
                                     Consumer<Integer> consumer) {
        String str = supplier.get();
        if (isNumber.test(str)) {
            consumer.accept(converter.apply(str));
        } else {
            System.out.println("非法数字");
        }
    }

    private static void compare() {
        List<String> list = Arrays.asList("aaaaa", "dfdf", "bfwsgvsjfoife", "csdsjkand", "dskd");

        // 原始比较
        // Collections.sort(list, new Comparator<String>() {
        //     @Override
        //     public int compare(String o1, String o2) {
        //         return o1.compareTo(o2);
        //     }
        // });

        // 简化1
        // Collections.sort(list, (o1, o2) -> o1.compareTo(o2));


        // 简化
        list.sort(String::compareTo);

        System.out.println(list);
    }

    private static void baseLamdbaUse() {
        // 基础
        Calculate calculate = new CalculateNumber();
        int sum = calculate.sum(1, 2);
        System.out.println(sum);


        // 新
        Calculate c = (i, j) -> i*i + j*j;
        System.out.println(c.sum(1, 2));
    }

}
